package com.etc.myblog.biz;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;


import com.etc.myblog.dao.UserDao;
import com.etc.myblog.entity.User;
import com.etc.myblog.util.SessionFactoryUtil;

public class UserBiz {
	//根据用户姓名获取用户
	public User findUserByuserName(String userName) {
		User user = null;
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			UserDao dao = session.getMapper(UserDao.class);
			user = dao.findUserByuserName(userName);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			session.close();
		}
		return user;
	}
	//新建用户
	public void addUser(User user) {
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			UserDao dao = session.getMapper(UserDao.class);
			dao.addUser(user);
			session.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
	}
	//删除用户
	public void deleteUser(String userName) {
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			UserDao dao = session.getMapper(UserDao.class);
			dao.deleteUser(userName);
			session.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
	}
	//修改用户资料
	public void updateUser(User user) {
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			UserDao dao = session.getMapper(UserDao.class);
			dao.updateUser(user);
			session.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
	}
	
	//修改用户资料，没有图片的情况
	public void updateUserNopic(User user) {
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			UserDao dao = session.getMapper(UserDao.class);
			dao.updateUserNopic(user);
			session.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
	}
	
	//修改用户密码
	public void updateUserPwd(User user) {
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			UserDao dao = session.getMapper(UserDao.class);
			dao.updateUserPwd(user);
			session.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
	}
	//根据id获取用户
	public User findById(int userId) {
		User user = new User();
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			UserDao dao = session.getMapper(UserDao.class);
			user = dao.findById(userId);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			session.close();
		}
		return user;
	}
	
	//获取积分大于100的用户
	public List<User> findAuthor() {
		List<User> list = new ArrayList<>();
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			UserDao dao = session.getMapper(UserDao.class);
			list = dao.findAuthor();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			session.close();
		}
		return list;
	}
	//查找所有
	public List<User> findAllUser(){
		List<User> list = new ArrayList<User>();
		SqlSession session = SessionFactoryUtil.getSession();
		try{
			UserDao dao = session.getMapper(UserDao.class);
			list = dao.findAllUser();
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		finally{
			session.close();
		}
		return list;
	}
	//更新权限
	public void updatePower(User user){
		SqlSession session = SessionFactoryUtil.getSession();
		try{
			UserDao dao = session.getMapper(UserDao.class);
			dao.updatePower(user);
			session.commit();
		}catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
	}
	//根据权限id查找
	public List<User> findByPowerId(int powerId){
		SqlSession session = SessionFactoryUtil.getSession();
		List<User> list = new ArrayList<User>();
		try{
			UserDao dao = session.getMapper(UserDao.class);
			list = dao.findByPowerId(powerId);
		}catch (Exception e) {
			System.out.println(e.getMessage());
			
		} finally {
			session.close();
		}
		return list;
	}
	//根据等级id查找
	public List<User> findByRankId(int rankId){
		SqlSession session = SessionFactoryUtil.getSession();
		List<User> list = new ArrayList<User>();
		try{
			UserDao dao = session.getMapper(UserDao.class);
			list = dao.findByRankId(rankId);
		}catch (Exception e) {
			System.out.println(e.getMessage());
			
		} finally {
			session.close();
		}
		return list;
	}
	
}
